package com.lanzx.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @author: lanzx
 * @description:
 * @date: 2020/8/20 16:52
 */
@Configuration
public class GuliMallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        // 配置跨域
        //  哪些请求头允许跨域
        corsConfiguration.addAllowedHeader("*");
        //  哪些请求方式允许跨域
        corsConfiguration.addAllowedMethod("*");
        //  哪些来源允许跨域
        corsConfiguration.addAllowedOrigin("*");
        //  是否携带cookie
        corsConfiguration.setAllowCredentials(true);
        source.registerCorsConfiguration("/**", corsConfiguration );
        return new CorsWebFilter(source);
    }
}
